package com.woniu.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.entity.Reg;
import com.woniu.entity.User1;
import com.woniu.mapper.UserDao;
import com.woniu.pojo.Student;
import com.woniu.pojo.User;
import com.woniu.service.UserService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jnlp.IntegrationService;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("user")
public class UserController {
    @Autowired
    UserService userService;


    /**
     * 跳转增加用户页面
     * */
    @RequestMapping("formAdd")
    public String fromAdd(Integer userStatus, Model model){
        model.addAttribute("userStatus", userStatus);
        System.out.println("UserController.fromAdd");
        return "WEB-INF/user/add";
    }

    /**
     * 跳转到用户列表
     * @return
     */
    @RequestMapping("formList")
    public String formList(){
        return "WEB-INF/user/userList";
    }

    //增加 用户
    @RequestMapping("add")
    @ResponseBody
    public String add(Reg user, HttpSession session){
        try {
            User1 us=(User1) session.getAttribute("user");
            if (user.getUserStatus().equals("1")) {
                addEmp(user);
            }else if (user.getUserStatus().equals("2")) {
                addStudent(user);
            }else{
                addCompany(user,us.getUid());
            }
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return "ok";
    }
    /**
     * 增加学生
     * @param user
     */
    private void addStudent(Reg user){
        userService.addStudent(user);
    }

    private void addCompany(Reg user ,Integer uid) {
        userService.addCompany(user,uid);
    }

    private int addEmp(Reg user) {
        int i = userService.addEmp(user);
        return i;
    }

    //删除用户
    @RequestMapping("deleteById")
    public void deleteById(Integer userId){
        userService.deleteById(userId);
    }
    //删除多个用户
    @RequestMapping("deleteByExample")
    public void deleteByExample(Integer[] userId){
        userService.deleteByExample(userId);
    }
    //修改用户
    @ResponseBody
    @RequestMapping("updateByExample")
    public  Map<String,Object> updateByExample(User user, HttpSession session){
        Map<String,Object> map=new HashMap<>();

        try {
            User1 user1 = (User1) session.getAttribute("user");
            user.setUserId(user1.getUser().getUserId());
            userService.updateByExample(user);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }
        return map;

    }

    /**
     * 得到所有用户
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("findAll")
    @ResponseBody
    public Map findAll(Integer page,Integer limit,User user){
        Map<String,Object> map= null;
        try {
            map = new HashMap<>();
            PageHelper.startPage(page,limit);
            List<User> all = userService.findAll(user);
            PageInfo<User> pageInfo=new PageInfo<>(all);
            map.put("data", pageInfo.getList());
            map.put("count",pageInfo.getTotal());
            map.put("code", "");
            map.put("msg",0);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return map;
    }
    @RequestMapping("resetPwd")
    @ResponseBody
    public String resetPwd(Integer userId){
        try {
            userService.resetPwd(userId);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return "ok";
    }
    //查看一个用户
    @RequestMapping("findOne")
    public  void  findOne(Integer userId){
        userService.findOne(userId);
    }

    /**
     * 判断用户名是否可用
     */
    @RequestMapping("judgeUserName")
    @ResponseBody
    public String judgeUserName(String userName){
        String s = userService.judgeUserName(userName);
        return s;
    }


  //展示个人信息
    @ResponseBody
    @RequestMapping("userInfo")
    public Map<String,Object>   userInfo(HttpSession session){
        Map<String,Object> map=new HashMap<>();

        try {
            User1 user = (User1) session.getAttribute("user");
           map = userService.findUserInfo(user);
            map.put("result","ok");
        } catch (Exception e) {
            map.put("result","error");
            e.printStackTrace();
        }

        return map;
    }
    @RequestMapping("studentInfo")
    public String studentInfo(){
        return "WEB-INF/info/studentInfo";
    }
    @RequestMapping("companyInfo")
    public String companyInfo(){
        return "WEB-INF/info/companyInfo";
    }
    @RequestMapping("empInfo")
    public String empInfo(){
        return "WEB-INF/info/empInfo";
    }


}
